/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Bussiness;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import DatabaseAccess.D_DBHelper;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import java.sql.ResultSet;
import java.util.Vector;
import javax.sql.rowset.CachedRowSet;
/**
 *
 * @author Administrator
 */
public class B_TableDataLoad {
/** Creates new form NewJFrame */
    private DefaultTableModel model=null;
    private D_DBHelper dbHelper=null;
    private ResultSet rs=null;
    public  CachedRowSet crs=null;
    public Vector ID;
    public Vector id;
    public  B_TableDataLoad(String fileConnection){
        dbHelper= new D_DBHelper();
        try{
        dbHelper.setConnectionUrl(fileConnection);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        if(!dbHelper.setupConnection()){
            JOptionPane.showMessageDialog(null,"Can not open the connection","Connection Dialog",JOptionPane.ERROR_MESSAGE);
        }
    }

    public  B_TableDataLoad(String dataBase,String userName,String passWord,int port){
        dbHelper= new D_DBHelper();
        if(!dbHelper.setupConnection(dataBase,userName,passWord,port)){
            JOptionPane.showMessageDialog(null,"Can not open the connection","Connection Dialog",JOptionPane.ERROR_MESSAGE);
        }
    }

    public DefaultTableModel setTableModel(JTable table){
        try{
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);
                while(crs.next()){
                    Vector<String> row=new Vector<String>();
                    for(int i=1;i<=table.getColumnCount();i++){
                        row.add(crs.getString(i));
                    }
                  model.addRow(row);
                 }
            }catch(Exception e){
                e.printStackTrace();
            }
        return model;
    }


    public DefaultTableModel setTableModel(JTable table,String spName){
        try{
                rs=dbHelper.executeProcedure(spName);
                
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);
                while(rs.next()){
                    Vector<String> row=new Vector<String>();
                    for(int i=1;i<=table.getColumnCount();i++){
                        row.add(rs.getString(i));
                    }
                  model.addRow(row);
                 }
            }catch(Exception e){
                e.printStackTrace();
            }
        return model;
    }
    
     public DefaultTableModel setTableModel(JTable table,String spName,Vector parameter){
        try{
                rs=dbHelper.executeProcedure(spName, parameter);
                id=new Vector();
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);

                while(rs.next()){
                    Vector<String> row=new Vector<String>();
                    id.add(rs.getInt(1));
                    for(int i=2;i<=table.getColumnCount()+1;i++){
                        row.add(rs.getString(i));
                    }
                  model.addRow(row);
                 }
            }catch(Exception e){
                e.printStackTrace();
            }
        return model;
    }
     
    
     public DefaultTableModel setTableModelParam(JTable table,String spName,Vector parameter){
        try{
                crs=dbHelper.executeProcedureQueryCachedRowSet(spName,parameter);
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);
                while(crs.next()){
                    Vector<String> row=new Vector<String>();
                    for(int i=1;i<=table.getColumnCount();i++){
                        row.add(crs.getString(i));
                    }
                  model.addRow(row);
                 }
            }catch(Exception e){
                e.printStackTrace();
            }
        return model;
        
    }
     
    public DefaultTableModel getComplaintOfCustomer(JTable table,String spName,Vector parameter){
        try{
                rs=dbHelper.executeProcedure(spName, parameter);
                ID=new Vector();
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);
                
                while(rs.next()){
                    Vector<String> row=new Vector<String>();
                    ID.add(rs.getInt(1));
                    //System.out.println(rs.getString(1));
                    for(int i=2;i<=table.getColumnCount();i++){
                        row.add(rs.getString(i));
                    }
                  model.addRow(row);
                  
                 }
            }catch(Exception e){
                e.printStackTrace();
            }

        return model;
        
    }


    public DefaultTableModel getComplaintOfCustomer(JTable table,String spName,int Id){
        try{
                rs=dbHelper.executeProcedure(spName, Id);
                //ID=new Vector();
                id=new Vector();
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);

                while(rs.next()){
                    Vector<String> row=new Vector<String>();
                    //ID.add(rs.getInt(1));
                    id.add(rs.getInt(1));
                    //System.out.println(rs.getString(1));
                    //for(int i=2;i<=table.getColumnCount();i++){
                        row.add(rs.getString(2));
                        row.add(rs.getString(3));
                        //}
                  model.addRow(row);

                 }
            }catch(Exception e){
                e.printStackTrace();
            }

        return model;

    }
    
    public DefaultTableModel setTableModel1(JTable table,String spName){
        try{
                rs=dbHelper.executeProcedure(spName);
                ID=new Vector();
                Vector<String> column=new Vector<String>();
                for(int i=0;i<table.getColumnCount();i++)
                    column.add(table.getColumnName(i));
                model=new DefaultTableModel(column,0);
                
                while(rs.next()){
                    Vector<String> row=new Vector<String>();
                    ID.add(rs.getString(1));
                     System.out.println("Size"+ID.size());
                    for(int i=2;i<=table.getColumnCount()+1;i++){
                        row.add(rs.getString(i));
                    }
                  model.addRow(row);
                 }
            }catch(Exception e){
                e.printStackTrace();
            }
        return model;
    }


}
